package com.sky.controller.admin;

import com.ejlchina.searcher.SearchResult;
import com.sky.dto.CategoryDTO;
import com.sky.entity.Category;
import com.sky.service.CategoryService;
import com.sky.web.controller.BaseController;
import com.sky.web.domain.AjaxResult;
import com.sky.web.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;

/**
 * 分类管理
 */
@Slf4j
@Api(tags = "分类相关接口")
@RestController
@RequestMapping("/admin/category")
public class CategoryController extends BaseController {

    @Autowired
    private CategoryService categoryService;


    /**
     * 分类分页查询
     * @param dto 查询信息
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("分类分页查询")
    public TableDataInfo list(CategoryDTO dto){
        log.info("分页查询：{}", dto);
        SearchResult<Category> list = categoryService.selectCategoryList(dto);
        return getDataTable(list);
    }

    /**
     * 分类类别查询全部
     * @param dto 查询信息
     * @return
     */
    @GetMapping("/listByType")
    @ApiOperation("分类类别查询全部")
    public AjaxResult listByType(CategoryDTO dto){
        log.info("分类类别查询全部：{}", dto);
        List<Category> list = categoryService.selectCategoryListByType(dto);
        return AjaxResult.success(list);
    }

    /**
     * 根据id查询分类信息
     * @param id 分类id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询分类信息")
    public AjaxResult getInfo(@PathVariable Long id){
        return AjaxResult.success(categoryService.selectCategoryById(id));
    }


    /**
     * 新增分类
     * @param dto 分类信息
     * @return
     */
    @PostMapping
    @ApiOperation("新增分类")
    @CacheEvict(cacheNames = "categoryCache",allEntries = true)
    public AjaxResult save(@RequestBody CategoryDTO dto){
        log.info("新增分类：{}", dto);
        return toAjax(categoryService.insertCategory(dto));
    }

    /**
     * 修改分类
     * @param dto 分类信息
     * @return
     */
    @PutMapping
    @ApiOperation("修改分类")
    @CacheEvict(cacheNames = "categoryCache",allEntries = true)
    public AjaxResult update(@RequestBody CategoryDTO dto){
        return toAjax(categoryService.updateCategory(dto));
    }

    /**
     * 删除分类
     * @param ids id集合
     * @return
     */
    @DeleteMapping("/{ids}")
    @ApiOperation("删除分类")
    @CacheEvict(cacheNames = "categoryCache",allEntries = true)
    public AjaxResult deleteById(@PathVariable Long[] ids){
        log.info("删除分类：{}", Arrays.asList(ids));
        return toAjax(categoryService.deleteCategoryByIds(ids));
    }
}
